package com.lg.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.product.entity.Brand;
import com.lg.product.service.IBrandService;
import com.lg.product.service.ICategoryBrandService;
import com.lg.product.service.impl.CategoryBrandServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService iBrandService;
    @Autowired
    private ICategoryBrandService iCategoryBrandService;

    /**
     *  分页查询
     * @param brand
     * @param page
     * @return
     */
    @RequestMapping("pageInfo")
    public IPage<Brand> pageInfo(Brand brand, Page page){
        return iBrandService.pageInfo(brand, page);
    }

    /**
     * 删除品牌表 同时 删除品牌和分类中间表
     * @param id
     * @return
     */
    @RequestMapping("deleteBrand")
    public int deleteBrand(Integer id){
        //品牌表
        iBrandService.deleteBrand(id);
        //品牌和分类中间表
        iCategoryBrandService.deleteCategoryBrand(id.longValue());
        return 1;
    }

    /**
     * 品牌 添加
     * @param brand
     * @return
     */
    @RequestMapping("addBrand")
    public int addBrand(@RequestBody Brand brand){
        if(brand.getUpadadd()==null){
            iBrandService.addBrand(brand);
        }
        iBrandService.updateBrand(brand);
        return 1;
    }

    /**
     * 品牌 回显
     * @param id
     * @return
     */
    @RequestMapping("listId")
    public Brand listId(Integer id){
        Brand brand = iBrandService.listId(id);
        return brand;
    }

    /**
     * 品牌 修改
     * @param brand
     * @return
     */
    @RequestMapping("updateBrand")
    public int updateBrand(@RequestBody Brand brand){
        int i = iBrandService.updateBrand(brand);
        return i;
    }

}
